﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using ZZQFrameWork;

///// <summary>
///// 装备item类型
///// </summary>
//public enum Enum_EquipItemType
//{
//    None,
//    Head,//头盔
//    Necklace,//项链
//    Clothes,//衣服
//    Pants,//裤子
//    Shoes,//鞋
//    Glove,//手套
//    Weqpon,//武器
//    Ring//戒指
//}

public class EquipInfoPanel : MonoBehaviour
{
    /// <summary>
    /// 装备资源图集
    /// </summary>
    private UIAtlas equipAtlas;

    public UIAtlas EquipAtlas
    {
        get
        {
            if (equipAtlas == null)
            {
                equipAtlas = UIAtlasManager.Instance.GetUIAtlasByName("Equip_Atlas");
            }
            return equipAtlas;
        }
    }

    /// <summary>
    /// 装备栏集合
    /// </summary>
    private List<EquipItem> equipItemList=new List<EquipItem>();

    /// <summary>
    /// 当前选择的装备栏
    /// </summary>
    private EquipItem currentEquipItem;

    /// <summary>
    /// 当前提示装备的装备栏
    /// </summary>
    private EquipItem currentGuideEquipItem;

    void Awake()
    {
        EquipItem[] items = gameObject.GetComponentsInChildren<EquipItem>(true);
        for (int i = 0; i < items.Length; i++)
        {
            EquipItem item = items[i];
            item.SetEquipInfoPanel(this);
            equipItemList.Add(item);
            EventTriggerListener.Get(items[i].gameObject).SetEventHandle(Enum_TouchEventType.OnClick, OnEquipItemButtonClick);
        }
    }

    void OnEnable()
    {
        EventDispatcher.AddListener<GoodsVO>(Events.CharacterEvent.OnEquipInfoPanelShowItemByEquipType, OnEquipInfoPanelShowItemByEquipType);
        EventDispatcher.AddListener<GoodsVO>(Events.CharacterEvent.OnWearEquip,OnWearEquip);
        EventDispatcher.AddListener<Enum_SubGoodsType>(Events.CharacterEvent.OnDropEquip, OnDropEquip);
        RefreshPanel();
    }

    void OnDisable()
    {
        EventDispatcher.RemoveListener<GoodsVO>(Events.CharacterEvent.OnEquipInfoPanelShowItemByEquipType, OnEquipInfoPanelShowItemByEquipType);
        EventDispatcher.RemoveListener<GoodsVO>(Events.CharacterEvent.OnWearEquip, OnWearEquip);
        EventDispatcher.RemoveListener<Enum_SubGoodsType>(Events.CharacterEvent.OnDropEquip, OnDropEquip);
    }

    /// <summary>
    /// 根据玩家在背包中点击的装备类型显示对应的装备栏
    /// </summary>
    /// <param name="goods"></param>
    private void OnEquipInfoPanelShowItemByEquipType(GoodsVO goods)
    {
        if(currentEquipItem!=null)currentEquipItem.SetItemClicked(false);
        if (goods.goodsType != Enum_GoodsType.Equip)
        {
            if (currentGuideEquipItem != null)
            currentGuideEquipItem.GuideEquip(false);
            return;
        }
        EquipItem item = equipItemList.Find(delegate(EquipItem obj)
        {
            return obj.equipItemType == goods.subGoodsType;
        });
        if (item != null)
        {
            if(currentGuideEquipItem!=null)currentGuideEquipItem.GuideEquip(false);
            currentGuideEquipItem = item;
            currentGuideEquipItem.GuideEquip(true);
        }
    }

    /// <summary>
    /// 装备格子点击
    /// </summary>
    /// <param name="_listener"></param>
    /// <param name="_args"></param>
    /// <param name="_params"></param>
    private void OnEquipItemButtonClick(GameObject _listener, object _args, object[] _params)
    {
        if (currentGuideEquipItem != null)
        {
            currentGuideEquipItem.SetItemClicked(false);
        }
        EquipItem item = _listener.GetComponent<EquipItem>();
        if (currentEquipItem != null)
        {
            currentEquipItem.SetItemClicked(false);
            if (currentEquipItem == item&&item.Data!=null)
            {
                ModuleManager.Instance.Get<CharacterModule>().DropEquip(item.Data.subGoodsType);
                currentEquipItem = null;
                return;
            }
        }
        currentEquipItem = item;
        currentEquipItem.SetItemClicked(true);
        if(item.Data!=null)
        EventDispatcher.TriggerEvent(Events.CharacterEvent.OnGoodsTipsShow,item.Data);
    }

    /// <summary>
    /// 穿装备
    /// </summary>
    /// <param name="goods"></param>
    private void OnWearEquip(GoodsVO goods)
    {
        Debug.Log("穿装备，装备类型："+goods.subGoodsType);
        for (int i = 0; i < equipItemList.Count; i++)
        {
            if (equipItemList[i].equipItemType == goods.subGoodsType)
            {
                equipItemList[i].Data = goods;
                return;
            }
        }
    }

    /// <summary>
    /// 脱装备
    /// </summary>
    /// <param name="type"></param>
    private void OnDropEquip(Enum_SubGoodsType type)
    {
        //Debug.Log("进入脱装备设置，装备类型："+type);
        //Debug.Log("当前装备数量："+equipItemList.Count);
        for (int i = 0; i < equipItemList.Count; i++)
        {
            //Debug.Log("装备类型为：" + equipItemList[i].equipItemType);
            if (equipItemList[i].equipItemType == type)
            {
                equipItemList[i].Data = null;
                return;
            }
        }
    }

    void RefreshPanel()
    {
        List<GoodsVO> list = ModuleManager.Instance.Get<CharacterModule>().mainRole.equipList;
        for (int i = 0; i < list.Count; i++)
        {
            EquipItem item = equipItemList.Find((obj) =>
            {
                return obj.equipItemType == list[i].subGoodsType;
            });
            item.Data = list[i];
        }
    }

}